package com.zlf.sb.demo.design.ali.proxy;

import com.zlf.sb.demo.design.ali.templete.DataCheckRequestDTO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

/***
 *@title DataCheckUtils
 *@description <TODO description class purpose>
 *@author wgc_j
 *@version 1.0.0
 *@create 2025-02-13 15:36
 **/
@Service
public class DataCheckUtils {
    ExecutorService DATA_CHECK_THREAD_POOL = Executors.newCachedThreadPool();

    public <T> void parallelCheckByFromResCodes(DataCheckRequestDTO dataCheckRequestDTO, List<String> fromResCodes, Consumer<String> checkOperation) {
        List<CompletableFuture> futureList = Collections.synchronizedList(new ArrayList<>());
        fromResCodes.forEach(fromResCode -> {
            CompletableFuture future = CompletableFuture.runAsync(() -> {
                try {
                    checkOperation.accept(fromResCode);
                } catch (Exception e) {
                     //recordError(dataCheckRequestDTO.getTaskId(), e);
                }
            },DATA_CHECK_THREAD_POOL);
            futureList.add(future);
        });
        //等待所有线程执行完毕
        futureList.forEach(future -> {
            try {
                future.get();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        });

    }
}
